package com.projet.instamiam;

import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.database.CursorWrapper;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.Toast;

public class ContactListActivity extends Activity implements OnClickListener {

	private Cursor c;
	private Button monBouton;

	private long img_id;
	
	public void onCreate(Bundle b) {
		super.onCreate(b);
		
		setContentView(R.layout.activity_contact_list);
		Intent i = this.getIntent();
		this.img_id = i.getLongExtra("com.projet.instamiam.message", (long)-1);

		c = managedQuery(ContactsContract.Contacts.CONTENT_URI, null, null,
				null, null);

		int[] to = new int[] { android.R.id.text1 };

		SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
				android.R.layout.simple_list_item_multiple_choice, c,
				new String[] { ContactsContract.Contacts.DISPLAY_NAME }, to);

		ListView lv = (ListView) findViewById(R.id.list);
		lv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
		lv.setAdapter(adapter);

		Cursor associate = getContentResolver().query(
				Uri.withAppendedPath(InstaMiamContract.Annotation.CONTENT_URI, "picture/"+this.img_id),
				new String[] { InstaMiamContract.Annotation.PEOPLE_ID },
				InstaMiamContract.Annotation.PICTURE_ID+"=?",
				new String[] { this.img_id+"" }, null);
		if (associate.getCount() >0){
			for (int j = 0; j < lv.getAdapter().getCount(); j++) {
				CursorWrapper item = (CursorWrapper) lv.getItemAtPosition(j);
				int item_id = item.getInt(item.getColumnIndex(ContactsContract.Contacts._ID));
				associate.moveToFirst();
				do {
					int contact_id = associate.getInt(associate.getColumnIndex(InstaMiamContract.Annotation.PEOPLE_ID));
					if (item_id == contact_id) {
						lv.setItemChecked(j, true);
					}
				} while (associate.moveToNext());
			}
		}
		monBouton = (Button) findViewById(R.id.buttonLayout);
		monBouton.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		
		if (this.img_id >= 0) {
			getContentResolver().delete(Uri.withAppendedPath(InstaMiamContract.Annotation.CONTENT_URI,
					"picture/"+this.img_id), InstaMiamContract.Annotation.PICTURE_ID+"=?", new String[] {
					this.img_id+""
			});
			ContentValues values = new ContentValues();
			values.put(InstaMiamContract.Annotation.PICTURE_ID, this.img_id);
			switch (v.getId()) {
	
			case R.id.buttonLayout:
				ListView liste = (ListView) findViewById(R.id.list);
				long[] maliste = liste.getCheckedItemIds();
				for (int i = 0; i < maliste.length; i++) {
					c = managedQuery(ContactsContract.Contacts.CONTENT_URI, null, ContactsContract.Contacts._ID+"= ?",
							new String[]{String.valueOf(maliste[i])}, null);
					while (c.moveToNext()) {
						Integer people_id = c.getInt(c.getColumnIndex(ContactsContract.Contacts._ID));
						values.put(InstaMiamContract.Annotation.PEOPLE_ID, people_id);
						getContentResolver().insert(Uri.withAppendedPath(InstaMiamContract.Annotation.CONTENT_URI,
								"picture/"+this.img_id), values);
						values.remove(InstaMiamContract.Annotation.PEOPLE_ID);
					}
				}
				
				Intent intent = new Intent(ContactListActivity.this, AnnotationActivity.class);
				intent.putExtra("com.projet.instamiam.message", this.img_id);

				try {
					ContactListActivity.this.startActivity(intent);
				} catch (ActivityNotFoundException e) {
					Toast.makeText(ContactListActivity.this, "Erreur",
							Toast.LENGTH_SHORT).show();
				}
				
				break;
			default:
				break;
			}
		}

	}
}
